<?php

require_once("../../class2.php");
require_once(HEADERF);
		$user_id = USERID;
		$user_name = USERNAME;
require_once("./includes/require_all.php");

ramka("open");

?>
<script type="text/javascript" src="./js/main.js"></script>
<?php
//--------------------------------------------------------------------
//------------------- SEARCH EMGINE ----------------------------------
//--------------------------------------------------------------------
//------------------- GET and POST section ---------------------------
$CMS_file = $_GET['name'];
$page_class = "PlayON";
CMS_activitySave("search.php");

 if(isset($_POST['search'])){
	if(isset($_GET['go'])){
		$searching=$_POST['searching'];
		$find=$_POST['find'];
		$field=$_POST['field'];
		if (isset($_POST['capses'])) $chk1='checked';
		if (isset($_POST['wholewords'])) $chk2='checked';
		if ($_POST['field']=='english') {$chk3='selected';}
		elseif ($_POST['field']=='content') $chk4='selected';
	}
 }
//------------------- HTML visualization of the search engine ---------
echo <<<HTML
		 <div>
		 <div class="forumheader4 textWHITE unselectable"><u><b>Търсачка</b></u></div>
		 <form name="search" method="post" action="search.php?game=$CMS_game&go=yes" class="forumheader textGREY" style="font-weight:normal;">
		 <b class='unselectable'>Намери: </b><input type="text" name="find" class="tbox" value='$find'/><b class='unselectable'> в </b> 
		 <Select NAME="field" class="tbox">
		 <Option VALUE="english" $chk3>Оригиналния текст</option>
		 <Option VALUE="content" $chk4>Преведените реплики</option>
		 </Select>
		 <input type="hidden" name="searching" value="yes" />
		 <input type="submit" name="search" value="Намери" class="button"/>
		 <div class='unselectable'>
			 <input type="checkbox" name="capses" value="yes" $chk1>Големи/малки букви
			 <input type="checkbox" name="wholewords" value="yes" $chk2>Цели думи
		 </div>
		 </form>
		 </div>
HTML;
//------------------- Check for submit and an error ------------------
 if (isset($searching) AND $searching =="yes") 
 {
	echo "<center class='forumheader4 textWHITE unselectable'><u><b>Резултати</b></u></center>"; 
	if ($find == "") 
	{ 
		echo "Забравихте да въведете ключова дума.<br>"; 
		echo "</div>";
		require_once(FOOTERF);
		exit; 
	}
	if (USERNAME === "USERNAME" OR $user_rights["ADD"]!=1) {
		echo "Нямате право да използвате този ресурс.<br>"; 
		echo "</div>";
		require_once(FOOTERF);
		exit; 
	}
//------------------- Filtering --------------------------------------	 
	$find = str_replace(" ", "%", mysql_real_escape_string(trim(preg_replace("/[^a-zA-Zа-яА-Я 0-9]+/u", " ", strip_tags($find)))));
//------------------- SQL search queries -----------------------------	 
	if (isset($_POST['capses'])) {
		$query= "SELECT * FROM $text_table WHERE $field LIKE '%".$find."%' COLLATE utf8_bin";
		if (isset($_POST['wholewords'])) {
			$query= "SELECT * FROM $text_table WHERE $field REGEXP '[[:<:]]".$find."[[:>:]]' COLLATE utf8_bin"; }
	} else {
		$find = strtoupper($find); 
		$query= "SELECT * FROM $text_table WHERE upper($field) LIKE '%".$find."%'";
		if (isset($_POST['wholewords'])) {
			$query= "SELECT * FROM $text_table WHERE upper($field) REGEXP '[[:<:]]".$find."[[:>:]]'"; }
	} $data = mysql_query($query); 
//------------------- Result display ---------------------------------	 
	$num= 1;
	
	echo "<div id='sresult'><TABLE style='float:left;' CELLSPACING='0' CELLPADDING ='4' WIDTH='100%'>";
	while($result = mysql_fetch_array( $data )) 
	{ 
		$result['content'] = trim($result['content']);
		$result['content'] = preg_replace("/\n/", " ", $result['content']);
		//--------------------------------------------------------------------
			$file_name= $result['file_name'];
			if (!isset($arr[$file_name])) {
				$first= mysql_fetch_array(mysql_query("SELECT id FROM $text_table WHERE file_name = '".mysql_real_escape_string($file_name)."' AND content <> '' LIMIT 1"));
				$arr[$file_name]= $first['id'];
			} unset ($first);
			$first= (int)$arr[$file_name]; $present= (int)$result['id']; $inner= $present-$first; if ($inner==0) {$inner=1;}
			$wites= mysql_num_rows(mysql_query("SELECT id FROM $text_table WHERE id > $first AND content = '' AND id < $present"));
			$count= $inner-$wites;
			$page= ceil($count/25);
			unset ($count);
			if ($num%2==1) { $class= "cmsRow1"; } else { $class= "cmsRow2"; }
		echo "<tr>";
		//--------------------------------------------------------------------
		$exploded_kwrd_srch= explode("%",$find);
		foreach ($exploded_kwrd_srch as $word){
			$exploded_kwrd_rplc[]="<span class='textRED'>$word</span>";
			}
		$result['english-echo']= str_ireplace($exploded_kwrd_srch,$exploded_kwrd_rplc,$result['english']);
		//--------------------------------------------------------------------
		if ($field === "english") {
			echo "<td class='forumheader4' WIDTH='2%' ALIGN='middle'>";
			echo "<a href='viewtext.php?game=$CMS_game&name=$file_name&page=$page#row_".$result['text_id']."' class='button unselectable textWHITE' title='Отвори страницата с репликата'>$page</a></td><td class='forumheader4'><span class='textYELLOW'>".$result['english-echo']."</span><br>";
			if ($result['english']!=$result['content'])
				{
					if(check_class($page_class)){
						echo "<div id='edit_".$result['text_id']."' style='display: inline;'><span style='textBLACK' class='unselectable'>➥ </span><span class='textGREY'>".$result['content']." </span><img src='./icons/edit_icon.png' alt='Редактирай превод' style='cursor: pointer;' onclick=\"edit_text('".$result['text_id']."', '".$result['file_name']."', '".$result['content']."', '".$CMS_game."')\"></div>";
					}
					else{
						echo "<span class='textBLACK unselectable'>➥ </span><span class='textGREY'>".$result['content']."</span><br>";
					}
				}
			echo "</td></tr>";
		}
		if ($field === "content") {
			echo "<td class='forumheader4' WIDTH='2%' ALIGN='middle'>";
			echo "<a href='viewtext.php?game=$CMS_game&name=$file_name&page=$page#row_".$result['text_id']."' class='button unselectable textWHITE' title='Отвори страницата с репликата'>$page</a><br></td>";
			echo "<td class='forumheader4'>";
			if(check_class($page_class)){
				echo "<div id='edit_".$result['text_id']."' style='display: inline;'><span class='textGREY'>".$result['content']." </span><img src='./icons/edit_icon.png' alt='Редактирай превод' style='cursor: pointer;' onclick=\"edit_text('".$result['text_id']."', '".$result['file_name']."', '".$result['content']."', '".$CMS_game."')\"></div>";
			}
			else{
				echo "<span class='textGREY unselectable'>".$result['content']."</span>";
			}
			if ($result['english']!==$result['content'])
				{
					echo "<br><span class='textBLACK unselectable'>➥ </span><span class='textYELLOW'>".$result['english-echo']."</span><br>";
				}
			echo "</td></tr>";
		}
		$num++;
	}
	echo "</table></div>";
	$anymatches=mysql_num_rows($data);
	echo "<div class='forumheader' style='padding-top:5px; clear:both;'>";
	if ($anymatches == 0) 
	{ 
		echo "Не бяха намерени реплики съдържащи ключовата дума.<br>"; 
	} 
	echo "<span class='unselectable'><b>Вие потърсихте: </b><span class='textWHITE'>".str_replace("%"," ",$find)."</span></span>"; 
	echo "</div>";
 } 
//------------------- SEARCH EMGINE END ------------------------------
//--------------------------------------------------------------------

ramka("close");

require_once(FOOTERF);
?> 